package com.itools.core.service.impl;

import com.itools.core.param.FmsUploadTokenParam;
import com.itools.core.result.FmsMultipartFileResult;
import com.itools.core.service.FileHandleService;
import com.itools.core.service.MinioClientService;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.Callable;

/**
 * @description:
 * @author: XUCHANG
 * @create: 2021-03-28 15:51
 */
public class FmsFileUploadMinioTask implements Callable<FmsMultipartFileResult> {

    /**
     * 上传的文件
     */
    private MultipartFile file;
    /**
     * 文件的访问的token获取的信息
     */
    private FmsUploadTokenParam param;
    /**
     * 生成的UUID
     */
    private String uniqueId;
    /**
     * 文件夹目录
     */
    private String fileStoreRootPath;

    private FileHandleService fileHandleService;

    private MinioClientService minioClientService;

    public FmsFileUploadMinioTask(MultipartFile file, FmsUploadTokenParam param, String uniqueId, String fileStoreRootPath, FileHandleService fileHandleService, MinioClientService minioClientService) {
        this.file = file;
        this.param = param;
        this.uniqueId = uniqueId;
        this.fileStoreRootPath = fileStoreRootPath;
        this.fileHandleService = fileHandleService;
        this.minioClientService = minioClientService;
    }

    @Override
    public FmsMultipartFileResult call() {
        String originalFilename = file.getOriginalFilename();
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."),originalFilename.length());
        fileHandleService.createAndPersistDBRecord(file,param,uniqueId);
        minioClientService.putObject(fileStoreRootPath,file,uniqueId+fileSuffix);
        FmsMultipartFileResult fmsMultipartFileResult = new FmsMultipartFileResult();
        fmsMultipartFileResult.setFileName(file.getOriginalFilename());
        fmsMultipartFileResult.setUniqueId(uniqueId);
        return fmsMultipartFileResult;
    }

}
